Skip to content

CSHARP-1378: Make BulkWrite enumerate requests argument only once #1298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024

Conversation

sanych-sun
Copy link
Member

No description provided.

@sanych-sun sanych-sun requested a review from a team as a code owner March 19, 2024 22:05
@sanych-sun sanych-sun requested review from adelinowona and removed request for a team March 19, 2024 22:05
@@ -174,7 +174,7 @@ public class Acknowledged : BulkWriteResult<TDocument>

// constructors
/// <summary>
/// Initializes a new instance of the <see cref="Acknowledged" /> class.
/// Initializes a new instance of the <see cref="BulkWriteResult{TDocument}.Acknowledged" /> class.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not related to the PR itself, but it fixes some warnings we got on build

@@ -218,27 +218,30 @@ public override BulkWriteResult<TDocument> BulkWrite(IEnumerable<WriteModel<TDoc
public override BulkWriteResult<TDocument> BulkWrite(IClientSessionHandle session, IEnumerable<WriteModel<TDocument>> requests, BulkWriteOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(session, nameof(session));
Ensure.IsNotNull(requests, nameof(requests));
if (!requests.Any())
Ensure.IsNotNull((object)requests, nameof(requests));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unnecessary cast to object needed to make code analysis happy, otherwise it says we might be doing multiple enumeration of the requests


var result = async ? await subject.BulkWriteAsync(wrappedRequests) : subject.BulkWrite(wrappedRequests);

result.Should().NotBeNull();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically we testing here is there were no exception, as wrappedRequests will throw on second attempt to enumerate.

@adelinowona adelinowona requested a review from BorisDog March 21, 2024 18:47
Copy link
Contributor

@adelinowona adelinowona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -234,7 +234,7 @@ internal static MongoBulkWriteException<TDocument> FromCore(MongoBulkWriteOperat

return new MongoBulkWriteException<TDocument>(
ex.ConnectionId,
BulkWriteResult<TDocument>.FromCore(ex.Result, processedRequests),
BulkWriteResult<TDocument>.FromCore(ex.Result, processedRequests.ToList()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider ToArray


namespace MongoDB.Driver.TestHelpers
{
public static class OnlyOnceEnumerable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider counting enumerations instead of throwing here.

@sanych-sun sanych-sun requested a review from BorisDog March 28, 2024 23:33
Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (!requests.Any())
Ensure.IsNotNull((object)requests, nameof(requests));

var requestList = requests.ToArray();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: requestsArray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants